package com.github.ki4so.sso.service;

import org.springframework.beans.factory.annotation.Autowired;

import com.github.ki4so.sso.api.SSOService;
import com.github.ki4so.sso.dto.CredentialDto;
import com.github.ki4so.sso.dto.RegisterDto;
import com.github.ki4so.sso.dto.SendSmsDto;
import com.github.ki4so.sso.dto.UserInfo;
import com.github.ki4so.sso.model.User;
import com.github.ki4so.sso.repository.UserRepository;

public class SSOServiceImpl implements SSOService {
	
	@Autowired
	private UserRepository userRepository;

	@Override
	public UserInfo login(CredentialDto dto) {
		User user = userRepository.getByUsernameAndPassword(dto.getUsername(), dto.getPassword());
		if (user == null) {
			return null;
		}
		UserInfo userInfo = new UserInfo();
		userInfo.setId(user.getId().longValue());
		userInfo.setUsername(user.getUsername());
		userInfo.setUsernick(user.getUserNick());
		return userInfo;
	}

	@Override
	public UserInfo Register(RegisterDto dto) {
		User user = new User();
		user.setUsername(dto.getUsername());
		user.setPassword(dto.getPassword());
		user.setUserNick("aaaa");
		userRepository.addUser(user);
		
		UserInfo userInfo = new UserInfo();
		userInfo.setId(user.getId().longValue());
		userInfo.setUsername(user.getUsername());
		userInfo.setUsernick(user.getUserNick());
		
		return userInfo;
	}

	@Override
	public String sendSmsCode(SendSmsDto dto) {
		//TODO: mock
		
		return "success";
	}

}
